package com.view;

import java.io.IOException;
import java.util.*;
import javax.microedition.lcdui.*;

public class SplashScreen extends Canvas implements Runnable {
    private Display display;
    private Displayable next;
    private Timer timer = new Timer();
    
    private Image backgroundImg;
    
    private boolean ready;
    
    private String text = "carregando";
    
    Thread t;
    
    public SplashScreen(Display display, Displayable next){
        this.display = display;
        this.next = next;
        try {
            
            backgroundImg = Image.createImage("/imgs/semFilaIntro_80p.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        t = new Thread(this);
        t.start();
        
        display.setCurrent( this );
    }
    
    protected void keyPressed( int keyCode ){
        dismiss();
    }
    
    protected void paint( Graphics g ){
        g.drawImage(backgroundImg,0,0,g.TOP|g.LEFT);        
        g.setColor(0x000000);
        g.setFont(Font.getFont(Font.FONT_STATIC_TEXT,Font.STYLE_BOLD,Font.SIZE_LARGE));
        g.drawString(text,getWidth()* 2/5,getHeight() - 30 , g.TOP|g.LEFT);
    }
    
    protected void showNotify(){
        timer.schedule( new CountDown(), 5000 );
    }
    
    private void dismiss(){
        timer.cancel();
        t.yield();
        display.setCurrent( next );
    }
    
    public void run() {
        while(true) {
            try {
                if(text.equalsIgnoreCase("carregando...")) {
                    text = "carregando";
                } else {
                    text += ".";
                }
                t.sleep(250);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            repaint();
            
        }
    }
    
    private class CountDown extends TimerTask {
        public void run(){
            dismiss();
        }
    }
}